package com.example.weatherapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {


    private TextView tvCity;
    private TextView tvTemp;
    private Button btnRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // 初始化UI组件
        tvCity = findViewById(R.id.tvCity);
        tvTemp = findViewById(R.id.tvTemp);
        btnRefresh = findViewById(R.id.btnRefresh);


        // 初始加载天气数据
        updateWeather();



        // 设置刷新按钮点击事件
        btnRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateWeather();
            }
        });
    }

    // 模拟天气数据更新
    private void updateWeather() {
        String[] cities = {"北京市", "上海市", "广州市", "深圳市", "成都市"};
        String randomCity = cities[new Random().nextInt(cities.length)];
        int randomTemp = 15 + new Random().nextInt(16); // 15~30℃之间的随机温度
        tvCity.setText(randomCity);
        tvTemp.setText(randomTemp + "℃");
    }
}